package com.example;
import java.util.*;

public class CinemaSystem {
    static List<User> users;
    static Map<String, User> userMap= new HashMap<>();  // 新增用户映射
    static List<Movie> movies;
    static List<Screening> screenings;
    static List<Order> orders;
    static List<ScreeningRoom> rooms;
    static List<String> logs;
    static List<Snack> snacks;  
    static User currentUser = null;
    static Set<String> userIds = new HashSet<>(); // 新增ID集合
    public static void main(String[] args) {
        initializeData();
        showMainMenu();
        saveAllData();
    }

    private static void initializeData() {
        DataManager.initDatabase();
        // 加载用户数据并创建映射
        users = DataManager.loadUsers();
        for (User user : users) {
            userMap.put(user.getUsername(), user);
            userIds.add(user.getUserId()); // 记录所有用户ID
        }

        // 提取放映厅初始化逻辑
        initializeScreeningRooms();

        // 加载电影数据
        movies = DataManager.loadMovies();
        // 加载场次数据
        screenings = DataManager.loadScreenings(movies, rooms);
        // 加载订单数据
        orders = DataManager.loadOrders();
        // 加载日志数据
        logs = DataManager.loadLogs();
        snacks = DataManager.loadSnacks();  // 加载零食数据
    }

    // 新增：独立的放映厅初始化方法
    private static void initializeScreeningRooms() {
        rooms = new ArrayList<>();
        rooms.add(new ScreeningRoom("黄金厅", 8, 10));
        rooms.add(new ScreeningRoom("白银厅", 10, 12));
        rooms.add(new ScreeningRoom("翡翠厅", 7, 8));
        rooms.add(new ScreeningRoom("钻石厅", 7, 8));
        rooms.add(new ScreeningRoom("宝石厅", 8, 9));
    }

    private static void saveAllData() {
        DataManager.saveUsers(users);
        DataManager.saveMovies(movies);
        DataManager.saveScreenings(screenings);
        DataManager.saveOrders(orders);
        DataManager.saveLogs(logs);
        DataManager.saveSnacks(snacks);  // 保存零食数据
        System.out.println("数据已成功保存到文件！");
    }

    private static void showMainMenu() {
        while (true) {
            System.out.println("\n=== 万东电影院管理系统 ===");
            System.out.println("1. 管理员登录");
            System.out.println("2. 经理登录");
            System.out.println("3. 前台登录");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作: ");

            int choice = Input.getValidIntInput(0, 3); // 限制输入0-3

            switch (choice) {
                case 1:
                    login("管理员");
                    break;
                case 2:
                    login("经理");
                    break;
                case 3:
                    login("前台");
                    break;
                case 0:
                    System.out.println("感谢使用万东电影院管理系统，再见！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }

    private static void login(String role) {
        System.out.println("\n=== " + role + "登录 ===");
        System.out.print("请输入用户名: ");
        String username = Input.getValidStringInput();
        System.out.print("请输入密码: ");
        String password = Input.getValidStringInput();

        String passwordHash = MD5Util.encrypt(password);
        User user = userMap.get(username);  // 通过映射快速查找用户

        if (user != null 
            && user.getPasswordHash().equals(passwordHash) 
            && user.getRole().equals(role)) {
            currentUser = user;
            logs.add("[登录成功] 角色: " + role + " 用户名: " + username + " 时间: " + new Date());  // 记录成功日志
            System.out.println("登录成功，欢迎 " + role + " " + username + "！");

            // 强制修改初始密码
            if (user.mustChangePassword()) {
                System.out.print("您需要修改初始密码，请输入新密码: ");
                String newPassword = Input.getValidStringInput();
                user.setPasswordHash(MD5Util.encrypt(newPassword));
                user.setMustChangePassword(false);
                System.out.println("密码修改成功！");
            }

            if (role.equals("管理员")) {
                Admin admin = new Admin(user.getUserId(), user.getUsername(), user.getPasswordHash(), user.getRole(), user.getRegistrationTime(), user.getPhoneNumber());
                admin.showMenu();  
            } else if (role.equals("经理")) {
                Manager manager = new Manager(user.getUserId(), user.getUsername(), user.getPasswordHash(), user.getRole(), user.getRegistrationTime(), user.getPhoneNumber());
                manager.showMenu();  
            } else if (role.equals("前台")) {
                Receptionist receptionist = new Receptionist(user.getUserId(), user.getUsername(), user.getPasswordHash(), user.getRole(), user.getRegistrationTime(), user.getPhoneNumber());
                receptionist.showMenu();  
            }
            return;
        }

        logs.add("[登录失败] 角色: " + role + " 用户名: " + username + " 时间: " + new Date());  // 记录失败日志
        System.out.println("登录失败，用户名、密码或角色不匹配！");
    }
}